package com.qd.panda.service.info;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdqidi.db.BaseService;
import com.qd.panda.mapper.info.ParkSpaceNumberMapper;
import com.qd.panda.model.ParkSpaceNumber;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 车位号
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class ParkSpaceNumberService extends BaseService<ParkSpaceNumberMapper, ParkSpaceNumber> {


    @Transactional(rollbackFor = Exception.class)
    public String getSysSpaceNumber(String parkId) {
        QueryWrapper<ParkSpaceNumber> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("park_id", parkId);
        queryWrapper.select("  IFNULL( max(space_number),0) as spaceNumber");
        ParkSpaceNumber parkSpaceNumber = getOne(queryWrapper);
        queryWrapper.clear();
        parkSpaceNumber.setSpaceNumber(parkSpaceNumber.getSpaceNumber() + 1);
        if (parkSpaceNumber.getSpaceNumber() > 0 && parkSpaceNumber.getSpaceNumber() < 10) {
            parkSpaceNumber.setSysSpaceNumber("000" + parkSpaceNumber.getSpaceNumber());
        } else if (parkSpaceNumber.getSpaceNumber() >= 10 && parkSpaceNumber.getSpaceNumber() < 100) {
            parkSpaceNumber.setSysSpaceNumber("00" + parkSpaceNumber.getSpaceNumber());
        } else if (parkSpaceNumber.getSpaceNumber() >= 100 && parkSpaceNumber.getSpaceNumber() < 1000) {
            parkSpaceNumber.setSysSpaceNumber("0" + parkSpaceNumber.getSpaceNumber());
        } else {
            parkSpaceNumber.setSysSpaceNumber(String.valueOf(parkSpaceNumber.getSpaceNumber()));
        }
        parkSpaceNumber.setParkId(parkId);
        save(parkSpaceNumber);
        return parkSpaceNumber.getSysSpaceNumber();
    }
}